<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h1 class="m-0 text-dark">用户管理</h1>
      </div><!-- /.col -->
      <div class="col-sm-6">
        <ol class="breadcrumb float-sm-right">
          <li class="breadcrumb-item"><a th:href="@{../admin}">首页</a></li>
          <li class="breadcrumb-item active">系统管理</li>
          <li class="breadcrumb-item active">用户管理</li>
        </ol>
      </div><!-- /.col -->
    </div><!-- /.row -->
  </div><!-- /.container-fluid -->
</div>
<!-- Main content -->
<section class="content">
  <div class="card card-info">
    <form id="searchForm" class="needs-validation form-inline" novalidate="">
      <div class="form-group">
        <label for="username">用户名:</label>
        <input type="text" class="form-control m-2" id="username">
      </div>
      <div class="form-group">
        <label for="mobile">电话:</label>
        <input type="tel" class="form-control m-2" id="mobile">
      </div>
      <div class="form-group">
        <button class="btn btn-primary ml-3" type="submit">查询</button>
        <button class="btn btn-default ml-2" type="reset">重置</button>
      </div>
    </form>
  </div>
  <div class="card card-body">
    <table class="table-sm" id="table"></table>
  </div>
</section>

<!-- 表单模态框 -->
<section id="formModalWrapper">
  <div class="modal fade" id="formModal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="formTitle"></h5>
          <button type="button" class="close" data-dismiss="modal">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <div id="formContent"></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
          <button type="button" class="btn btn-primary" id="saveOrUpdateBtn">保存
          </button>
        </div>
      </div>
    </div>
  </div>
</section>

<script>

  $(function () {

    window.operateEvents = {
      // 点击详情
      'click .btn_show': function (e, value, row, index) {
        showRow(row.id)
      }
    }

    var columns = [
      {
        field: 'id',
        title: 'id',
        align: "center"
      },
      {
        field: 'username',
        title: '用户名',
        align: "center"
      }, {
        field: 'mobile',
        title: '电话',
        align: "center",
      }, {
        field: 'roleName',
        title: '角色',
        align: "center",
      }, {
        field: 'createTime',
        title: '创建时间',
        align: "center",
      }, {
        field: 'operation',
        title: '操作',
        align: "center",
        events: operateEvents,
        formatter: operationFormatter
      }];



    function operationFormatter(value, row, index) {
      return '<button class="btn btn-info btn-sm btn_show">详情</button>';
    }

    var options = {
      id: "#table",
      url: '/sys/user',
      columns: columns,
      showRefresh: true,
      queryParams: queryParams
    }
    Core.initTable(options);

    function refreshTable() {
      Core.refreshTable("#table")
    }

    // 查询参数
    function queryParams(params) {
      return {
        page: params.pageNumber,
        size: params.pageSize,
        username: $("#username").val(),
        mobile: $("#mobile").val()
      };
    }

    // 查询
    $('#searchForm').submit(function (event) {
      event.preventDefault();
      event.stopPropagation();
      refreshTable();
    })

    // 显示用户详情
    function showRow(id) {
      Core.load("#formContent", "/load/user/" + id, function () {
        $('#formTitle').text('详情')
        $("#formModal").modal('show');
      }, 2);
    }

    // 更新
    $('#saveOrUpdateBtn').on('click', function () {
      var $f = $('#userForm');
      var valid = $f[0].checkValidity();
      var url = "/sys/user";
      var method = Core.putAjax;
      var data = {
        id: $f.find("input[name='id']").val(),
        username: $f.find("input[name='username']").val(),
        mobile: $f.find("input[name='mobile']").val(),
        roleName: $f.find("input[name='roleName']").val(),
        createTime: $f.find("input[name='createTime']").val(),
      }
      if (valid) {
        Core.mask("#saveOrUpdateBtn");
        method(url, data, function (data) {
          Core.unmask("#saveOrUpdateBtn");
          if (data.code === 1) {
            $("#formModal").modal("hide");
            refreshTable();
            layer.msg("保存成功");
          } else {
            layer.msg(data.message);
          }
        }, function () {
          Core.unmask("#saveOrUpdateBtn");
        })
      }
      $f.addClass("was-validated")
    })

  });

</script>